﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 28/12/2008
// Time: 14:05
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Data;
using csharp.common;
using System.Collections.Generic;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of Neighborhood.
	/// </summary>
	public class Neighborhood : DefaultClass, IClass
	{
		#region private declaration
			private String _city_id;
			private String _name;
			private String _zip_code;
			private String _create_date;
			private String _enable;
		#endregion
		
		public Neighborhood()
		{
			clear();
			gsql.table = "common.neighborhood";
			_sequence  = "common.neighborhood_neighborhood_id_seq";
		}
		
		#region properties
			public string cityId
			{
				get { return  _city_id; }
				set { _city_id = value; }
			}
			
			public string name 
			{
				get { return _name; }
				set { _name = value; }
			}
			
			public string zipCode
			{
				get { return  _zip_code; }
				set { _zip_code = value; }
			}
			
			public string createDate 
			{
				get { return _create_date; }
				set { _create_date = value; }
			}
			
			public Boolean enable 
			{
				get { return Convert.ToBoolean(_enable); }
				set { _enable = Convert.ToString(value); }
			}	
		#endregion
	
		public void clear()
		{
			_id          = String.Empty;
			_city_id		 = String.Empty;
			_name        = String.Empty;
			_zip_code	 = String.Empty;
			_create_date = String.Empty;
			_enable		 = Boolean.TrueString;			
		}
		
		public override void setGsql()
		{
			gsql.setValue(_id,    		 "neighborhood_id");
			gsql.setValue(_city_id, 	 "neighborhood_city_id");
			gsql.setValue(_name,  		 "neighborhood_name");
			gsql.setValue(_zip_code, 	 "neighborhood_zip_code");
			//gsql.setValue(_create_date, "neighborhood_create_date");
			gsql.setValue(_enable,      "neighborhood_enable");
		}
		
		public override void get(DataRow dr)
		{
			_id      	= dr["neighborhood_id"].ToString();
			_city_id		= dr["neighborhood_city_id"].ToString();
			_name       = dr["neighborhood_name"].ToString();
			_zip_code	= dr["neighborhood_zip_code"].ToString();
			_create_date= dr["neighborhood_create_date"].ToString();
			_enable     = dr["neighborhood_enable"].ToString();
		}
	}

	public class Neighborhoods : DefaultList, IList
	{
		public Neighborhoods()
		{
			gsql.table = "common.neighborhood";
		}
		
		
		/*public List<Neighborhood> items
		{
			get { return items; }
		}*/		
		
		public Neighborhood get(Int32 index)
		{
			return (Neighborhood)items[index];
		}
		
		public Neighborhood get()
		{
			return (Neighborhood)items[index];
		}		
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.additionalWhere = "ORDER BY neighborhood_name";
			
			return mountList();
		}
		
		public bool fillList(string cityId)
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.conditionalWhere= "neighborhood_city_id = "+cityId;
			gsql.additionalWhere = "ORDER BY neighborhood_name";
			
			return mountList();
		}
		
		private bool mountList()
		{
			Int32 tam;
			
			if (!gsql.execute())
				return false;
			
			tam = gsql.getRowCount();
			items.Clear();
			for (int i=0; i < tam; i++)
			{
				Neighborhood c = new Neighborhood();
				c.get(gsql.getRow(i));
				items.Add(c);
			}
			return true;
		}
		
		public bool searchByZipCode(String value)
		{
			int tam= items.Count;
			for (int i=0; i < tam; i++)
			{
				if (this.get(i).zipCode.Equals(value))
				{
					index = i;
					return true;
				}
			}
			return false;
		}
		
		//UNDONE: tirar, deixar somente o método acima, retornando bool
		//int search(String value);
		
		//void clear();		
	}
}

